package com.xiaotu.spider.maoyan.pageprocessor;
/***
 * 猫眼电影短评最热500条
 * @author Administrator
 *
 */

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;


import com.xiaotu.common.model.ShortCommentsModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.DateUtils;
import com.xiaotu.common.util.HttpUtils;
import com.xiaotu.common.util.KeywordEncode;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.UUIDUtils;
import com.xiaotu.spider.AUrlPageProcessor;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;

@Component("MaoYanMovieSmallCommentPageProcessor")
@Scope("prototype")
public class MaoYanMovieSmallCommentPageProcessor extends AUrlPageProcessor{

	@Override
	public void process(Page page) {
		
		Map<String, Object> movieMap = page.getRequest().getExtras();
		
		switch (this.getPageDataType(page))
        {
            case Constants.PageDataType.TV_COMMENT:
                this.downLoadData(page,movieMap);
                break;
            case "getMovieId":
                this.addRequest(page,movieMap);
                break;
            case "searchPage":
            	this.searchPage(page,movieMap);
            	break;
            default:
                break;
        }
	}

	
	//用于获取数据
	private void downLoadData(Page page,Map<String, Object> movieMap) {
		//电影名
		String movieName = movieMap.get("movieName").toString();
		//获取需要爬去的数据
		String jsonStr = HttpUtils.httpGetJSONP(page.getRequest().getUrl());
		JSONObject jsonObj = JSONObject.fromObject(jsonStr);
		if(null!=jsonObj.getString("data")){
			//初始化
			JSONArray commentsData = new JSONArray();
			JSONObject jsonData = JSONObject.fromObject(jsonObj.getString("data"));
			if(null!=jsonData.get("hotComments")){
				commentsData = jsonData.getJSONArray("hotComments");
			}
			if(null!=jsonData.get("comments")){
				commentsData = jsonData.getJSONArray("comments");
			}
			List<ShortCommentsModel> shortCommentsModelList = new ArrayList<ShortCommentsModel>();
			for(int i=0;i<commentsData.size();i++){
				JSONObject eachObj = commentsData.getJSONObject(i);
				//内容
				String comment = eachObj.getString("content")==null?null:eachObj.getString("content").replaceAll("[^(0-9a-zA-Z\u4e00-\u9fa5\\p{P}\r\n )]", "");
				//评论时间
				String commentTime = eachObj.getString("time")==null?null:DateUtils.fomartDateToString(new Date(Long.parseLong(eachObj.getString("time"))),DateUtils.SECONDS_FORMAT);
				//点赞数
				Integer thumbsUpNum = eachObj.getString("upCount")==null?null:Integer.parseInt(eachObj.getString("upCount"));
				//踩数
				Integer thumbsDownNum = null;
				//再评论数
				Integer reComentsNum = eachObj.getString("replyCount")==null?null:Integer.parseInt(eachObj.getString("replyCount"));
				//星级或评分
				Integer score = eachObj.getString("score")==null?null:Integer.parseInt(eachObj.getString("score"));
				//数据id
				String id = eachObj.getString("id");
				ShortCommentsModel shortCommentsModel = new ShortCommentsModel(
						UUIDUtils.getId(),movieName,"猫眼",comment,commentTime,
						thumbsUpNum,thumbsDownNum,reComentsNum,score);
				shortCommentsModelList.add(shortCommentsModel);
			}
			page.putField(ShortCommentsModel.class.getName(), shortCommentsModelList);
		}
	}
	
	//搜索页面
	private void searchPage(Page page,Map<String, Object> movieMap){
		//搜索是否有结果
		if(!page.getHtml().xpath("//div[@class='search-result-box']").get().contains("empty-list")){
			String movieName = page.getHtml().xpath("//input[@class='kw']/@value").get();
			if(StringUtils.isNotBlank(movieName)){
				movieMap.put("movieName", movieName);
				//获取总页数
				int maxPageNum = 0;
				if(StringUtils.isNotBlank(page.getHtml().xpath("//ul[@class='list-pager']/").get())){
					List<Integer> pageNumList = new ArrayList<Integer>();
					
					for(String liStr :page.getHtml().xpath("//ul[@class='list-pager']/li").all()){
						Html liHtml = new Html(liStr);
						int pageNum = 0;
						try {
							pageNum = Integer.parseInt(liHtml.xpath("//a/text()").get());
						} catch (Exception e) {
							continue;
						}
						pageNumList.add(pageNum);
					}
					Collections.sort(pageNumList);
					maxPageNum = pageNumList.get(pageNumList.size()-1);
				}
				
				List<String> requestUrlList = new ArrayList<String>();
				
				if(maxPageNum > 0){
					for(int i=1;i<=maxPageNum;i++){
						requestUrlList.add("https://maoyan.com/query?kw="+KeywordEncode.encode(movieName)+"&offset="+((i-1)*20));
					}
				}else{
					requestUrlList.add("https://maoyan.com/query?kw="+KeywordEncode.encode(movieName)+"&offset="+0);
				}
				PageUtils.addTargetRequests(page,requestUrlList,movieMap);
			}
		}
	}
	
	
	
	//用于添加新的请求
	private void addRequest(Page page,Map<String, Object> movieMap) {
		//获取当前电影的movieId
		//当前电影名字
		String movieName = movieMap.get("movieName")==null?"":movieMap.get("movieName").toString();
		List<String> ddStrList = page.getHtml().xpath("//dl[@class='movie-list']/dd").all();
		for(String ddStr : ddStrList){
			Html ddHtml = new Html(ddStr);
			String eachMovieName = ddHtml.xpath("//div[@class='channel-detail movie-item-title']/@title").get();
			if(movieName.equals(eachMovieName)){
				movieMap.put("movieName", eachMovieName);
				//获取movieId
				String movieId = ddHtml.xpath("//div[@class='channel-detail movie-item-title']/a/@href").get();
				movieId = movieId.substring(movieId.lastIndexOf("/")+1);
				//将热评的10条先保存
				PageUtils.addTargetRequest(page,"http://m.maoyan.com/review/v2/comments.json?movieId="+movieId+"&userId=-1&offset=0&limit=15&ts=0&type=1",movieMap);
				//最新评论
				for(int i=0;i<=480;i+=20){
					PageUtils.addTargetRequest(page,"http://m.maoyan.com/review/v2/comments.json?movieId="+movieId+"&userId=-1&offset="+i+"&limit=20&ts=0&type=2",movieMap);
				}
				break;
			}
		}
		
	}
		
	
	@SuppressWarnings("deprecation")
	@Override
	protected String[] getStartRequestUrls() {
		String[] urlArr = {
				"https://maoyan.com/query?kw="+KeywordEncode.encode("巨齿鲨"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("我不是药神"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("前任3：再见前任"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("影"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("战狼2"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("网络谜踪"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("西虹市首富"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("流浪地球"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("头号玩家"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("三块广告牌"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("疯狂的外星人"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("捉妖记2"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("幕后玩家"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("一出好戏"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("超时空同居"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("来电狂响"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("邪不压正"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("无间道"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("神奇动物在哪里"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("纯洁心灵·逐梦演艺圈"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("让子弹飞"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("无名之辈"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("动物世界"),
				"https://maoyan.com/query?kw="+KeywordEncode.encode("叶问外传：张天志")
		};
		return urlArr;
	}
	
	@Override
	protected String getSpiderType() {
		return Constants.SpiderTaskType.MOVIE_MAOYAN_COMMENT;
	}

	@Override
	public String getPageDataType(Page page) {
		String url = page.getUrl().toString();
		String dataType = "";
		if (url.contains("http://m.maoyan.com/review/v2/comments.json?movieId=")){
			dataType = Constants.PageDataType.TV_COMMENT;
	    }
        else if (url.contains("https://maoyan.com/query?kw=")){
        	dataType = "searchPage";
        	if (url.contains("offset")){
            	dataType = "getMovieId";
            }
        };
        return dataType;
	}
}
